<?php

namespace App\Controller\Admin;

use App\Entity\Comment;
use App\Entity\Conference;
use App\Message\CommentMessage;
use App\Repository\CommentRepository;
use App\Repository\ConferenceRepository;
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\Registry;

/**
 * @Route("/admin")
 * Class AdminController
 * @package App\Controller
 */
class AdminController extends BaseController
{
    /**
     * @Route("/conference/list/{page}",name="admin_conference_list",requirements={"page":"\d+|"})
     * @param int $page
     * @param ConferenceRepository $conferenceRepository
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function ConferenceIndex(ConferenceRepository $conferenceRepository,$page=0)
    {
        $limit = 2;
        $this->parameters['conferences'] = $conferenceRepository->findBy([],['id'=>'desc'],$limit,$limit*$page);
        $this->parameters['previous'] = $limit*$page - $limit;
        $this->parameters['next'] = min(count($this->parameters['conferences']), $limit*$page + $limit);
        return $this->render("admin/list/conference.html.twig",$this->parameters);
    }

    /**
     * @Route("/comment/list/{slug}/{page}",name="admin_comment_list",requirements={"page":"\d+|"})
     * @param int $page
     * @param Conference $conference
     * @param CommentRepository $commentRepository
     * @return Response
     */
    public function commentListAction(Conference $conference,CommentRepository $commentRepository,$page=0)
    {
        $limit = 2;
        $this->parameters['conference'] = $conference;
        $this->parameters['comments'] = $commentRepository->findBy(['conference'=>$conference],['id'=>'desc'],$limit,$limit*$page);
        $this->parameters['previous'] = $limit*$page - $limit;
        $this->parameters['next'] = min(count($this->parameters['comments']), $limit*$page + $limit);
        return $this->render("admin/list/comment.html.twig",$this->parameters);
    }

    /**
     * @Route("/comment/review/{id}", name="review_comment")
     * @param Request $request
     * @param Comment $comment
     * @param Registry $registry
     * @param MessageBusInterface $bus
     * @return Response
     */
    public function reviewComment(Request $request, Comment $comment, Registry $registry,MessageBusInterface $bus)
    {
        $accepted = !$request->query->get('reject');

        $machine = $registry->get($comment);
        if ($machine->can($comment, 'publish')) {
            $transition = $accepted ? 'publish' : 'reject';
        } elseif ($machine->can($comment, 'publish_ham')) {
            $transition = $accepted ? 'publish_ham' : 'reject_ham';
        } else {
            return new Response('Comment already reviewed or not in the right state.');
        }

        $machine->apply($comment, $transition);
        $this->container->get('doctrine')->getManager()->flush();

        if ($accepted) {
            $reviewUrl = $this->generateUrl('review_comment', ['id' => $comment->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
            $bus->dispatch(new CommentMessage($comment->getId(), $reviewUrl));
        }

        return $this->render('admin/review.html.twig', [
            'transition' => $transition,
            'comment' => $comment,
        ]);
    }

    /**
     * @Route("/http-cache/{uri<.*>}", methods={"PURGE"})
     */
    public function purgeHttpCache(KernelInterface $kernel, Request $request,string $uri)
    {
        if ('prod' === $kernel->getEnvironment()) {
            return new Response('KO', 400);
        }

        $store = (new class($kernel) extends HttpCache {})->getStore();
        $store->purge($request->getSchemeAndHttpHost().'/'.$uri);

        return new Response('Done');
    }


}